﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http;
using System.Net;
using System.Web;
using System.Web.Security;
using WS.EKA.Core.DI;
using WS.EKA.ServiceInterface;
using WS.EKA.Utilities.Consts;
using WS.EKA.Utilities.EncryptTool;
using Newtonsoft.Json;
using WS.EKA.Utilities.Extensions;
using WS.EKA.Portal.Filters;
using WS.EKA.Model;
using AttributeRouting.Web.Http;
using WS.EKA.Utilities.Core;

namespace WS.EKA.Portal.Controllers
{
    [AuthencationFilter(true)]
    public class AccountController : ControllerBase
    {
        private IAccountService accountService;
        private ICookieRepository Cookie;

        public AccountController(IAccountService accountService, ICookieRepository cookie)
        {
            this.accountService = accountService;
            this.Cookie = cookie;
        }

        // GET /api/account/5
        [GET("api/account/{loginid}")]
        public Account Get(string loginid)
        {
            return accountService.GetByLoginId(loginid);
        }

        [PUT("api/account/setauth/{loginid}")]
        public HttpStatusCode GetUserAutoLogin(string loginid)
        {
            var user = accountService.GetByLoginId(loginid);
            if (user != null)
            {
                FormAuth(loginid);
                return HttpStatusCode.OK;
            }
            else
            {
                return HttpStatusCode.NotFound;
            }
        }
        [GET("api/account/userexist/{loginid}")]
        public bool UserExist(string loginId)
        {
            return accountService.UserExist(loginId);
        }

        [HttpPut]
        public HttpStatusCode Login(Account account)
        {
            var user = accountService.Authentication(account);
            if (user != null)
            {
                SetAuthCookie(account);
                return HttpStatusCode.OK;
            }
            else
            {
                return HttpStatusCode.NotFound;
            }
        }
       
        //register

        [POST("api/account/Regist/")]
        public HttpStatusCode Regist(Account account)
        {
            int result = accountService.Add(account);
            if (result > 0)
            {
                SetAuthCookie(account);
                return HttpStatusCode.Accepted;
            }
            return HttpStatusCode.ExpectationFailed;
        }

        /// <summary>
        /// Logout
        /// </summary>
        /// <param name="id"></param>
        [POST("api/account/LogOut/")]
        public HttpStatusCode LogOut(string id)
        {
            Cookie.Remove(UserConsts.LoginId);
            FormsAuthentication.SignOut();
            return HttpStatusCode.OK;

        }

        #region
        private void SetAuthCookie(Account account)
        {
            string userName = account.MemLoginID;
            int sTimeOutHours = UserConsts.SessionTimeOutHours.ConfigValue().ToInt32();
            string userNameHash = EncryptFactory.Create().ToMD5(userName + UserConsts.WSMobile.ConfigValue());
            //user info
            if (account.RememberMe)
            {
                sTimeOutHours = 30 * 24;
            }
            Cookie.Set(UserConsts.LoginId, userName, sTimeOutHours);
            FormAuth(userName);
        }
        private void FormAuth(string userName)
        {
            FormsAuthentication.SetAuthCookie(userName, false, FormsAuthentication.FormsCookiePath);
        }

        #endregion
    }
}
